# Laravel Vue Admin 项目初始化脚本 (Windows PowerShell)
# 自动完成项目初始化，包括数据库迁移和种子数据填充

param(
    [switch]$Force = $false,  # 强制重置数据库
    [switch]$SkipMigrations = $false  # 跳过数据库迁移
)

Write-Host "=== Laravel Vue Admin 项目初始化脚本 ===" -ForegroundColor Cyan
Write-Host ""

# 1. 检查 PHP
Write-Host "[1/8] 检查 PHP 环境..." -ForegroundColor Yellow
try {
    $phpVersion = php -v 2>&1 | Select-Object -First 1
    if ($LASTEXITCODE -ne 0) {
        throw "PHP 未找到"
    }
    Write-Host "✓ $phpVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 错误: PHP 未找到或无法运行" -ForegroundColor Red
    Write-Host "请确保 PHP >= 8.1 已安装并添加到 PATH 环境变量" -ForegroundColor Yellow
    exit 1
}

# 2. 检查 Composer
Write-Host "[2/8] 检查 Composer..." -ForegroundColor Yellow
try {
    $composerVersion = composer --version 2>&1 | Select-Object -First 1
    if ($LASTEXITCODE -ne 0) {
        throw "Composer 未找到"
    }
    Write-Host "✓ $composerVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 错误: Composer 未找到" -ForegroundColor Red
    Write-Host "请安装 Composer: https://getcomposer.org/" -ForegroundColor Yellow
    exit 1
}

# 3. 检查 Laravel 应用
Write-Host "[3/8] 检查 Laravel 应用..." -ForegroundColor Yellow
if (-not (Test-Path "artisan")) {
    Write-Host "✗ 错误: 未找到 artisan 文件" -ForegroundColor Red
    Write-Host "请确保在 backend 目录下运行此脚本" -ForegroundColor Yellow
    exit 1
}
Write-Host "✓ Laravel 应用找到" -ForegroundColor Green

# 4. 检查并创建 .env 文件
Write-Host "[4/8] 检查环境配置文件..." -ForegroundColor Yellow
if (-not (Test-Path ".env")) {
    if (Test-Path ".env.example") {
        Write-Host "⚠ .env 文件不存在，正在从 .env.example 创建..." -ForegroundColor Yellow
        Copy-Item ".env.example" ".env"
        Write-Host "✓ .env 文件已创建" -ForegroundColor Green
        Write-Host "⚠ 请编辑 .env 文件，配置数据库连接信息！" -ForegroundColor Yellow
    } else {
        Write-Host "✗ 错误: .env.example 文件不存在" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "✓ .env 文件存在" -ForegroundColor Green
}

# 5. 生成 APP_KEY
Write-Host "[5/8] 检查并生成 APP_KEY..." -ForegroundColor Yellow
$envContent = Get-Content ".env" -Raw
if ($envContent -notmatch "APP_KEY=base64:") {
    Write-Host "⚠ APP_KEY 未设置，正在生成..." -ForegroundColor Yellow
    php artisan key:generate --force 2>&1 | Out-Null
    Write-Host "✓ APP_KEY 已生成" -ForegroundColor Green
} else {
    Write-Host "✓ APP_KEY 已设置" -ForegroundColor Green
}

# 6. 生成 JWT 密钥
Write-Host "[6/8] 检查并生成 JWT 密钥..." -ForegroundColor Yellow
$envContent = Get-Content ".env" -Raw
if ($envContent -notmatch "JWT_SECRET=") {
    Write-Host "⚠ JWT_SECRET 未设置，正在生成..." -ForegroundColor Yellow
    php artisan jwt:secret --force 2>&1 | Out-Null
    Write-Host "✓ JWT_SECRET 已生成" -ForegroundColor Green
} else {
    Write-Host "✓ JWT_SECRET 已设置" -ForegroundColor Green
}

# 7. 清理缓存
Write-Host "[7/8] 清理缓存..." -ForegroundColor Yellow
php artisan config:clear 2>&1 | Out-Null
php artisan cache:clear 2>&1 | Out-Null
php artisan route:clear 2>&1 | Out-Null
php artisan view:clear 2>&1 | Out-Null
Write-Host "✓ 缓存已清理" -ForegroundColor Green

# 8. 数据库迁移和种子
Write-Host "[8/8] 数据库迁移和种子数据..." -ForegroundColor Yellow

if ($SkipMigrations) {
    Write-Host "⚠ 跳过数据库迁移" -ForegroundColor Yellow
} else {
    # 检查数据库配置
    $envContent = Get-Content ".env" -Raw
    if ($envContent -notmatch "DB_DATABASE=") {
        Write-Host "✗ 错误: 数据库配置不完整" -ForegroundColor Red
        Write-Host "请编辑 .env 文件，配置数据库连接信息" -ForegroundColor Yellow
        exit 1
    }

    # 测试数据库连接
    Write-Host "  测试数据库连接..." -ForegroundColor Gray
    try {
        php artisan db:show 2>&1 | Out-Null
        if ($LASTEXITCODE -ne 0) {
            throw "数据库连接失败"
        }
        Write-Host "  ✓ 数据库连接成功" -ForegroundColor Green
    } catch {
        Write-Host "  ✗ 数据库连接失败" -ForegroundColor Red
        Write-Host "  请检查 .env 文件中的数据库配置" -ForegroundColor Yellow
        Write-Host "  数据库配置示例：" -ForegroundColor Yellow
        Write-Host "    DB_CONNECTION=mysql" -ForegroundColor White
        Write-Host "    DB_HOST=127.0.0.1" -ForegroundColor White
        Write-Host "    DB_PORT=3306" -ForegroundColor White
        Write-Host "    DB_DATABASE=laravel_vue_admin" -ForegroundColor White
        Write-Host "    DB_USERNAME=root" -ForegroundColor White
        Write-Host "    DB_PASSWORD=your_password" -ForegroundColor White
        exit 1
    }

    # 运行数据库迁移
    if ($Force) {
        Write-Host "  ⚠ 强制重置数据库（将删除所有数据）..." -ForegroundColor Yellow
        $confirm = Read-Host "  确定要继续吗？(yes/no)"
        if ($confirm -ne "yes") {
            Write-Host "  已取消" -ForegroundColor Yellow
            exit 0
        }
        Write-Host "  正在重置数据库..." -ForegroundColor Gray
        php artisan migrate:fresh --force 2>&1 | Out-Null
        if ($LASTEXITCODE -ne 0) {
            Write-Host "  ✗ 数据库迁移失败" -ForegroundColor Red
            exit 1
        }
        Write-Host "  ✓ 数据库已重置" -ForegroundColor Green
    } else {
        Write-Host "  正在运行数据库迁移..." -ForegroundColor Gray
        php artisan migrate --force 2>&1 | Out-Null
        if ($LASTEXITCODE -ne 0) {
            Write-Host "  ✗ 数据库迁移失败" -ForegroundColor Red
            Write-Host "  提示: 如果表已存在，可以使用 -Force 参数重置数据库" -ForegroundColor Yellow
            exit 1
        }
        Write-Host "  ✓ 数据库迁移完成" -ForegroundColor Green
    }

    # 运行数据库种子
    Write-Host "  正在填充初始数据（管理员账号、角色、权限、菜单）..." -ForegroundColor Gray
    php artisan db:seed --force --class=DatabaseSeeder 2>&1 | Out-Null
    if ($LASTEXITCODE -ne 0) {
        Write-Host "  ✗ 数据库种子失败" -ForegroundColor Red
        exit 1
    }
    Write-Host "  ✓ 初始数据填充完成" -ForegroundColor Green
}

Write-Host ""
Write-Host "=== 初始化完成！ ===" -ForegroundColor Green
Write-Host ""
Write-Host "默认管理员账号：" -ForegroundColor Cyan
Write-Host "  用户名: admin" -ForegroundColor White
Write-Host "  密码: admin123" -ForegroundColor White
Write-Host ""
Write-Host "下一步：" -ForegroundColor Cyan
Write-Host "  1. 启动后端服务器: php artisan serve" -ForegroundColor White
Write-Host "  2. 启动前端服务器: cd ../frontend && npm run dev" -ForegroundColor White
Write-Host "  3. 访问前端地址: http://localhost:5173" -ForegroundColor White
Write-Host ""

